

package com.qianchang.microfinance.view.mine;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.widget.Button;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.MD5Util;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.dm.library.utils.ViewUtils;
import com.dm.library.widgets.OneLineEditLinearLayout;
import com.dm.library.widgets.OneLineLinearLayout;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.api.InputTradPwdApi;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.bean.RechargeMaxAmountBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.mine.loan.LoanProgressActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 充值
 * 修 改 人: zhoulai
 * 修改时间: 2017/8/14 17:26
 */


public class RechargeActivity extends BaseActivity {

    private static final String TAG = "RechargeActivity";
    @BindView(R.id.tv_balance)
    DTextView tvBalance;
    @BindView(R.id.oll_bank_card)
    OneLineLinearLayout ollBankCard;
    @BindView(R.id.oll_recharge_amount)
    OneLineEditLinearLayout ollRechargeAmount;
    @BindView(R.id.btn_recharge)
    Button btnRecharge;
    private double intAmount;//充值金额
    public static final int RequestCode = 1001;
    private RechargeMaxAmountBean rechargeMaxAmountBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        setTitleName(getString(R.string.recharge));
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        ollRechargeAmount.setInputType(InputType.TYPE_CLASS_NUMBER);
        ollRechargeAmount.setMaxLength(8);
        ollRechargeAmount.setTextWatcher(new OneLineEditLinearLayout.TextWatcher() {
            @Override
            public void afterTextChanged(Editable s, int flag) {
                intAmount = StringUtils.toInt(s.toString(), 0);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();
        if (bundle != null) {
            MineUserInfo mineUserInfo = (MineUserInfo) bundle.getSerializable(MineUserInfo.class.getName());
            BankCardBean bankCardBean = (BankCardBean) bundle.getSerializable(BankCardBean.class.getName());
            refresh(bankCardBean);
            if (mineUserInfo != null) {
                tvBalance.setTextContent(FormatUtil.numKbPointFormat(mineUserInfo.getAvailable()));
            }
        }
        queryMaxAmount();
    }

    private void queryMaxAmount() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.QUERY_MAX_RECHARGE_AMOUNT, params, new RequestCallBack<RechargeMaxAmountBean>() {
            @Override
            public void onSuccess(JsonResult<RechargeMaxAmountBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    RechargeActivity.this.rechargeMaxAmountBean = response.getObject();
                } else {
                    ToastUtil.getInstant().show(RechargeActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(RechargeActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    @OnClick(R.id.btn_recharge)
    public void onViewClicked() {
        if (intAmount == 0) {
            ToastUtil.getInstant().show(RechargeActivity.this, getString(R.string.msg_input_rechargeAmount));
            return;
        }
        if (rechargeMaxAmountBean != null) {
            if (intAmount > rechargeMaxAmountBean.getMaxAmount() || intAmount < rechargeMaxAmountBean.getMinAmount()) {
                String msg = getString(R.string.msg_rechargeAmount_max_min)
                        + FormatUtil.numKbPointFormat(rechargeMaxAmountBean.getMinAmount())
                        + "-"
                        + FormatUtil.numKbPointFormat(rechargeMaxAmountBean.getMaxAmount())
                        + "元";
                ToastUtil.getInstant().show(RechargeActivity.this, msg);
                return;
            }

            //判断有无交易密码
            InputTradPwdApi.checkHasTradPwd(RechargeActivity.this, new InputTradPwdApi.PwdCallBack() {
                @Override
                public void commit(String password) {
                    submit(password);
                }
            });
        }
    }

    //在线充值
    private void submit(String password) {
        show();
        HttpParams params = new HttpParams();
        params.put("amount", intAmount);
//        params.put("fee", "0");//手续费 N
        params.put("tradePwd", MD5Util.string2MD5(password + ConstantAppInfo.Password.PASSWORD_SUFFIX));
        params.put("paidType", "3");  //	支付方式: 01.微信 02.支付宝 03.储蓄卡充值
//        params.put("thirdCompany", "03");//	N	第三方支付公司

        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.ONLINE_RECHARGE, params, new RequestCallBack<BankCardBean>() {

            @Override
            public void onSuccess(JsonResult<BankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
//                    startActivity(充值成功界面);
                    InputTradPwdApi.dismiss();
                    Bundle bundle = new Bundle();
                    bundle.putDouble(RechargeActivity.class.getName(), intAmount);
                    startActivityForResult(LoanProgressActivity.class, bundle, RequestCode);
                } else {
                    ToastUtil.getInstant().show(RechargeActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(RechargeActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }

    private void refresh(BankCardBean bankCardBean) {
        if (bankCardBean == null) {
            showDataError();
            return;
        }
        ollBankCard.setRightContent(ViewUtils.formatBankCard(bankCardBean.getBankCardNumber()));
        ollBankCard.setLeftContent(bankCardBean.getBankName());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RequestCode) {
            finish();
        }
    }

/*


 private void getPhoneCode(TextView tv) {
        showRemainTime(tv);
    }
       //显示获取手机验证码倒计时
    private void showRemainTime(final TextView tvGetPhoneCode) {
        tvGetPhoneCode.setEnabled(false);
        phoneDownTimer = new CountDownTimer(DEFAULT_TIME, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                int temp = (int) (millisUntilFinished / 1000);
                tvGetPhoneCode.setText(temp + "S");//getString(R.string.remain_time) +
                tvGetPhoneCode.setEnabled(false);
            }

            @Override
            public void onFinish() {
                tvGetPhoneCode.setText(getString(R.string.btn_get_verify_code));
                tvGetPhoneCode.setEnabled(true);
            }
        }.start();
    }

   private void sendSmscode(final TextView tv) {

        if (phone == null) {
            showDataError();
            return;
        }
        show();
        HttpParams params = new HttpParams();
        params.put("receiver", phone);
        params.put("verifyType", "0");
        params.put("busType", "9");

        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.SEND_IDENTIFY_CODE, params, new RequestCallBack<BankCardBean>() {

            @Override
            public void onSuccess(JsonResult<BankCardBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    getPhoneCode(tv);//点击是发送验证码
                } else {
                    ToastUtil.getInstant().show(RechargeActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(RechargeActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {

            }
        });
    }*/

}
